package com.dycong.common.leetcode.yi;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-08-26
 * Time: 16:41
 */
public class UnsortedSubarray {
    public static void main(String[] args) {
        System.out.println(findUnsortedSubarray(new int[]{5, 4, 3, 2, 1}));
    }

    public static int findUnsortedSubarray(int[] nums) {
        int numsClone[] = nums.clone();
        Arrays.sort(numsClone);
        int start = numsClone.length, end = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != numsClone[i]) {
                start = Math.min(start, i);
                end = Math.max(end, i);
            }

        }
        return (end - start >= 0) ? end - start + 1 : 0;
    }
}
